//
//  PywUserCenterApi.m
//  PywSdk
//
//  Created by smallwave on 16/8/11.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywUserCenterApi.h"
#import "AIFUserCenterManager.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywUserCenterModel.h"

@interface PywUserCenterApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywUserCenterApi

- (void)requestUserCenterWithSuccess:(Successd)success
                                fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    
    AIFUserCenterManager *manager = [AIFUserCenterManager manager];
    manager.delegate = self;
    [manager loadData];
}

#pragma mark - ETAPIManagerCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager
{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    
    if (resultCode == 200) {
        
        NSDictionary *data = [dict objectForKey:@"data"];
        PywUserCenterModel *userInfo = [[PywUserCenterModel alloc] initWithDict:data];
        
        if (self.successd) {
            self.successd(nil,userInfo);
        }
    } else {
        [self userCenterRequestFail];
    }
    
}
/**
 *  请求失败
 *
 *  @param manager
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    [self userCenterRequestFail];
}
/**
 *  失败
 */
- (void)userCenterRequestFail{
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

@end
